﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestProgress
{
    public partial class AjaxProgress : System.Web.UI.Page
    {
        private void beginProgress()
        {
            //根据ProgressBar.htm显示进度条界面
            string templateFileName = Path.Combine(Server.MapPath("."), "ProgressBar.htm");
            StreamReader reader = new StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("GB2312"));
            string html = reader.ReadToEnd();
            reader.Close();
            Response.Write(html);
            Response.Flush();
        }

        private void setProgress(int percent)
        {
            string jsBlock = "<script>SetPorgressBar('" + percent.ToString() + "'); </script>";
            Response.Write(jsBlock);
            Response.Flush();
        }

        private void finishProgress()
        {
            string jsBlock = "<script>SetCompleted();</script>";
            Response.Write(jsBlock);
            Response.Flush();
        }

        private void Page_Load(object sender, System.EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            beginProgress();

            for (int i = 1; i <= 100; i++)
            {
                setProgress(i);

                //此处用线程休眠代替实际的操作，如加载数据等
                System.Threading.Thread.Sleep(50);
            }

            finishProgress();
        } 


    }
}